Arduino board is designed for working with digital signal or square wave signal. If we want to generate sine wave, we have to do little effort, because analog output from arduino is not true analog but a PWM output which turn board on and off very frequently.
Here we use digital to analog converter to generate fine analog output. A digital to analog converter has a single output having number of digital input.
PARTS LIST OF YUNE LAYER USING ARDUINO
Resistor (all ¼-watt, ± 5% Carbon) |
R1 – R4, R8 = 10 KΩ
R5 – R7 = 4.7 KΩ R9 = 1MΩ VR1 = 100 KΩ |
Capacitors |
C1 = 100 µF/16V
C2 = 100nF |
Semiconductors |
Arduino Board = Arduino Diecimila or Uno or Duemilanove or clone board
IC1 = TDA7052 1W audio amplifier IC |
Miscellaneous |
LS1 = 8Ω loudspeaker |
Software of Tune Player Using Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
int dacPins[] = {2, 4, 7, 8}; int sin16[] = {7, 8, 10, 11, 12, 13, 14, 14, 15, 14, 14, 13, 12, 11, 10, 8, 7, 6, 4, 3, 2, 1, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6}; int lowToneDurations[] = {120, 105, 98, 89, 78, 74, 62}; // A B C D E F G int highToneDurations[] = { 54, 45, 42, 36, 28, 26, 22 }; // a b c d e f g // Scale //char* song = "A B C D E F G a b c d e f g"; // Jingle Bells //char* song = "E E EE E E EE E G C D EEEE F F F F F E E E E D D E DD GG E E EE E E EE E G C D EEEE F F F F F E E E G G F D CCCC"; // Jingle Bells - Higher char* song = "e e ee e e ee e g c d eeee f f f f f e e e e d d e dd gg e e ee e e ee e g c d eeee f f f f f e e e g g f d cccc"; void setup() { for (int i = 0; i < 4; i++) { pinMode(dacPins[i], OUTPUT); } } void loop() { int i = 0; char ch = song[0]; while (ch != 0) { if (ch == ' ') { delay(75); } else if (ch >= 'A' and ch <= 'G') { playNote(lowToneDurations[ch - 'A']); } else if (ch >= 'a' and ch <= 'g') { playNote(highToneDurations[ch - 'a']); } i++; ch = song[i]; } delay(5000); } void setOutput(byte value) { digitalWrite(dacPins[3], ((value & 8) > 0)); digitalWrite(dacPins[2], ((value & 4) > 0)); digitalWrite(dacPins[1], ((value & 2) > 0)); digitalWrite(dacPins[0], ((value & 1) > 0)); } void playNote(int pitchDelay) { long numCycles = 5000 / pitchDelay + (pitchDelay / 4); for (int c = 0; c < numCycles; c++) { for (int i = 0; i < 32; i++) { setOutput(sin16[i]); delayMicroseconds(pitchDelay); } } } |